# -*- coding: utf-8 -*-
"""
Created on Thu Sep 24 10:48:40 2020

@author: juliang
"""

from AlphaFactor import AlphaModel
import datetime

# In[0]： 参数设定
#stock_codes = '000905.SH'     # 投资资产的代码
stock_codes = '000905.SH_in_day'     # 投资资产的代码
#ind_codes = ['indi1','indi2','indi3','indi4','indi5']     # 因子代码
ind_codes = ['indi1_in_day','indi2_in_day']     # 因子代码
begdate = '20200910'      # 开始研究日期
enddate = '20200922'      # 最终研究日期

# In[1]: 因子筛选与合成
# 实例化模型类
am = AlphaModel(stock_codes,ind_codes,begdate,enddate,
                        period=1,bench_code='000905.SH',is_index = True,
                        include_st = False,include_suspend = False,
                        include_new_stock = False,ipo_days = 60,
                        model_type = 'score',combine_type = 'ic_ir'
                        )
# 不分组
indicator_outcome1 = am.run_single_indicator_analysis(ind_codes[0])   # 计算单因子统计数据
indicator_outcome2 = am.run_multi_indicators_analysis(ind_codes[:1])   # 计算多因子统计数据
#indicator_outcome3 = am.run_multi_indicators_analysis([ind_codes[0],ind_codes[2],ind_codes[4]])

# 分组
am.set_groupby()
indicator_outcome1 = am.run_single_indicator_analysis(ind_codes[0])   # 计算单因子统计数据
indicator_outcome2 = am.run_multi_indicators_analysis(ind_codes[0:4])   # 计算多因子统计数据

# In[2]: 根据因子进行选股
datestr = datetime.datetime(2020,10,1).strftime('%Y%m%d')
datelist=['20200828','20200904','20200911','20200918','20200922']
stock_num=30
select_ind=['indi1','indi2','indi3','indi4']
lookbackPeriod=5

#
## 生成时间轴
#def date_range(start, end, step=7, format="%Y%m%d"):
#    strptime, strftime = datetime.datetime.strptime, datetime.datetime.strftime
#    days = (strptime(end, format) - strptime(start, format)).days + 1
#    return [strftime(strptime(start, format) + datetime.timedelta(i), format) for i in range(0, days, step)]
#
## 生成2016-01-01至2016-12-31的所有时间点
#date_list = date_range("20200703", "20200918")
#
#datelist=date_list

datelist=list(am.stock.index)[4:]

'''按照打分法进行选股'''
am.set_model_type('score')
# 传入一个日期，不行业分类
stocklist = am.select_stocks_by_model(datestr,stock_num,by_group = False)
# 传入一个日期，行业分类
stocklist1 = am.select_stocks_by_model(datestr,stock_num,by_group = True)
# 传入一个日期序列，不行业分类
stock_choosed = am.select_stocks_by_model(datelist,stock_num,by_group = False,select_ind = None,lookbackPeriod = 50)
# 传入一个日期序列，行业分类
stock_choosed1 = am.select_stocks_by_model(datelist,stock_num,by_group = True,select_ind = None,lookbackPeriod = 50)

'''按照回归法进行选股'''
am.set_model_type('regress')
# 传入一个日期，不行业分类
stocklist = am.select_stocks_by_model(datestr,stock_num,by_group = False)
# 传入一个日期，行业分类
stocklist1 = am.select_stocks_by_model(datestr,stock_num,by_group = True)
# 传入一个日期序列，不行业分类
stock_choosed = am.select_stocks_by_model(datelist,stock_num,by_group = False,select_ind = None,lookbackPeriod = 50)
# 传入一个日期序列，行业分类
stock_choosed1 = am.select_stocks_by_model(datelist,stock_num,by_group = True,select_ind = None,lookbackPeriod = 50)

'''赋权重'''
stock_choosed_df = am.add_weight_to_stock_choose()
stock_choosed_df1=am.add_weight_to_stock_choose_minTE()

'''回测'''
am.back_test_choosen_stock(period='daily',with_weights=True) 
#这里的period是数据的周期，日频/周频/月频


'''数据保存'''
#stock_choosed_df.to_csv('stock_choosed.csv')

#测试用
#stock = am.stock.copy(deep = True)
#select_ind = am.select_ind
#select_factors = am.factors[select_ind]
#
#test_factor=am.factor









